//
//  AppDelegate+AppPush.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/7.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "AppDelegate+AppPush.h"
#define IOS_VERSION      [[[UIDevice currentDevice] systemVersion] floatValue]

@implementation AppDelegate (AppPush)

// MARK: - 启动注册远程通知------------------------------------
- (void)registerMyRemoteNotiApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"%f",IOS_VERSION);
    if (IOS_VERSION >= 10.0) {
        if (@available(iOS 10.0, *)) {
            UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
            [center setDelegate:self];
            UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
            [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
                if (granted) {
                    NSLog(@"注册成功");
                }else{
                    NSLog(@"注册失败");
                }
                
            }];
        } else {
            // Fallback on earlier versions
        }
    }else if (IOS_VERSION >= 8.0){
        UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |
        UIUserNotificationTypeSound |
        UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    // 注册获得device Token
    [application registerForRemoteNotifications];
}


// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSString *deviceTokenSt = [[[[deviceToken description]
                                 stringByReplacingOccurrencesOfString:@"<" withString:@""]
                                stringByReplacingOccurrencesOfString:@">" withString:@""]
                               stringByReplacingOccurrencesOfString:@" " withString:@""];
    //这里将token传送给服务器-----
    //
    NSLog(@"deviceTokenSt:%@",deviceTokenSt);
}

// 注册deviceToken失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"error -- %@",error);
    
}


//iOS10以上版本的处理
//在前台
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)){
    
    // 需要执行这个方法，选择是否提醒用户，有Badge、Sound、Alert三种类型可以设置
    //用户即使在前台，收到推送时通知栏也会出现，有声音和角标。如果去掉应用在前台有推送时并不会收到。
    completionHandler(UNNotificationPresentationOptionBadge|
                      UNNotificationPresentationOptionSound|
                      UNNotificationPresentationOptionAlert);
    
}



//>=ios10的版本，三种情况都会来到这个方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){
    //在没有启动本App时，收到服务器推送消息，下拉消息会有快捷回复的按钮，点击按钮后调用的方法，根据identifier来判断点击的哪个按钮
//    [self handlePushMessage:response.notification.request.content.userInfo];
    
    //处理推送过来的数据
    completionHandler();
}


//ios10以下版本
//远程推送APP在前台  或者是在后台再次返回前台 或者重新进入程序
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary * _Nonnull)userInfo fetchCompletionHandler:(void (^ _Nonnull)(UIBackgroundFetchResult))completionHandler{
    NSLog(@"didReceiveRemoteNotification:%@",userInfo);
    /*
     UIApplicationStateActive 应用程序处于前台
     UIApplicationStateBackground 应用程序在后台，用户从通知中心点击消息将程序从后台调至前台
     UIApplicationStateInactive 用用程序处于关闭状态(不在前台也不在后台)，用户通过点击通知中心的消息将客户端从关闭状态调至前台
     
     */
    //应用程序在前台给一个提示特别消息
    
    if (application.applicationState == UIApplicationStateActive) {
        //应用程序在前台
//        [self createAlertViewControllerWithPushDict:userInfo];
    }else{
        //其他两种情况，一种在后台程序没有被杀死，另一种是在程序已经杀死。用户点击推送的消息进入app的情况处理。
//        [self handlePushMessage:userInfo];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}


@end
